package cate.game.role.res.adverttisement;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDOperation;
import cate.common.table.res.watchadvertisement.row.CallRow;
import cate.common.table.res.watchadvertisement.row.OnlineRow;
import cate.common.table.res.watchadvertisement.row.QuickFightRow;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.res.MixRes;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import cate.game.role.res.adverttisement.msg.WatchAdvertisementInfoResp;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

public class RoleWatchAdvertisement extends FuncOpenChecker {
    @NoteField("快速作战看广告领奖次数")
    public int quickTimes;
    @NoteField("登录累计时长领奖次数")
    public int loginTimes;
    @NoteField("高抽看广告领奖次数")
    public int callTimes;
    @NoteField("今日累充金额")
    public long todayPaySum;
    @NoteField("前六天的充值金额")
    public List<Long> payList;
    @NoteField("本次登录之前今日累计登录时长")
    public long onlineTime;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (payList == null) {
            payList = new ArrayList<>();
        }
    }

    @Override
    public int getFuncId() {
        return GDFunc.ADVERTISEMENT_REWARD;
    }

    @Override
    public void onAfterOpen() {

    }

    public GameResult<RoleWatchAdvertisement> takeReward(int type) {
        GameResult<RoleWatchAdvertisement> r = new GameResult<>();
        r.data = this;
        if (!isOpen()) {
            return r.fail("功能未开启");
        }
        switch (type) {
            case 1:
                GameResult<RoleWatchAdvertisement> qr = quickReward();
                if (!qr.ok()) {
                    return r.fail(qr);
                }
                break;
            case 2:
                GameResult<RoleWatchAdvertisement> or = onlineReward();
                if (!or.ok()) {
                    return r.fail(or);
                }
                break;
            case 3:
                GameResult<RoleWatchAdvertisement> cr = callReward();
                if (!cr.ok()) {
                    return r.fail(cr);
                }
                break;
        }
        return r;
    }

    private GameResult<RoleWatchAdvertisement> quickReward() {
        GameResult<RoleWatchAdvertisement> r = new GameResult<>();
        if (this.quickTimes >= role.getGame().table.advertisement.quickFight.list.size()) {
            return r.fail("今日已无奖励");
        }
        QuickFightRow row = role.getGame().table.advertisement.quickFight.get(this.quickTimes + 1);
        if (row == null) {
            return r.fail("配置错误");
        }
        this.quickTimes++;
        MixRes res = new MixRes(row.reward);
        res.addAndDialog(role, true, GDOperation.ADV_QUICK_FIGHT, this.quickTimes);
        return r;
    }

    private GameResult<RoleWatchAdvertisement> onlineReward() {
        GameResult<RoleWatchAdvertisement> r = new GameResult<>();
        if (this.loginTimes >= role.getGame().table.advertisement.online.list.size()) {
            return r.fail("今日已无奖励");
        }
        OnlineRow row = role.getGame().table.advertisement.online.get(this.loginTimes + 1);
        if (row == null) {
            return r.fail("配置错误");
        }
        long totalOnlineTime = calTotalOnlineTime();
        if (totalOnlineTime < row.onlineTime) {
            return r.fail("登录时长不足");
        }
        this.loginTimes++;
        MixRes res = new MixRes(row.reward);
        res.addAndDialog(role, true, GDOperation.ADV_ONLINE, this.loginTimes);
        return r;
    }

    private GameResult<RoleWatchAdvertisement> callReward() {
        GameResult<RoleWatchAdvertisement> r = new GameResult<>();
        if (this.callTimes >= role.getGame().table.advertisement.call.list.size()) {
            return r.fail("今日已无奖励");
        }
        CallRow row = role.getGame().table.advertisement.call.get(this.callTimes + 1);
        if (row == null) {
            return r.fail("配置错误");
        }
        this.callTimes++;
        MixRes res = new MixRes(row.reward);
        res.addAndDialog(role, true, GDOperation.ADV_HIGH_CALL, this.callTimes);
        return r;
    }


    public void onPay(long num) {
        this.todayPaySum += num;
        noticeUpdate();
    }


    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        this.quickTimes = 0;
        this.loginTimes = 0;
        this.callTimes = 0;
        this.onlineTime = 0;
        updatePay();
        noticeUpdate();
    }

    public void updatePay() {
        if (payList.size() >= 6) {
            payList.remove(0);
        }
        payList.add(this.todayPaySum);
        this.todayPaySum = 0;
    }

    public void noticeUpdate() {
        role.sendNow(new WatchAdvertisementInfoResp(this));
    }

    @Override
    protected void offline() {
        super.offline();
        long now = System.currentTimeMillis();
        long zeroTime = TimeTool.getZeroTimeStamp(now);
        if (zeroTime > role.getStatus().loginTime) {
            this.onlineTime = now - zeroTime;
        } else {
            this.onlineTime += now - role.getStatus().loginTime;
        }
    }

    private long calTotalOnlineTime() {
        long now = System.currentTimeMillis();
        long zeroTime = TimeTool.getZeroTimeStamp(now);
        if (zeroTime > role.getStatus().loginTime) {
            return (this.onlineTime + now - zeroTime)/1000;
        } else {
            return (this.onlineTime + now - role.getStatus().loginTime)/1000;
        }
    }
}
